package com.lanhao.aot.elasticsearch;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;

import co.elastic.clients.elasticsearch._types.ElasticsearchException;
import co.elastic.clients.elasticsearch.indices.IndexState;

public class EsOptDemo {
	private static final Logger LOG = LoggerFactory.getLogger(EsOptDemo.class);
	public static void main(String[] args) {
		try {
			String indexName = "data_model";
			// 创建索引
//			boolean createIndex = ElasticsearchUtil.createIndex(indexName);
//			LOG.info("创建索引{}，结果{}", indexName, createIndex);
//			// 删除索引
//			boolean deleteIndex = ElasticsearchUtil.deleteIndex(indexName);
//			LOG.info("删除索引{}，结果{}", indexName, deleteIndex);
//			// 查询索引状态
//			IndexState indexState = ElasticsearchUtil.getIndexState(indexName);
//			LOG.info("查询索引状态信息，indexState={}", indexState.toString());
//			// 插入文档
//			DocumentModel data = new DocumentModel();
//			data.setName("可乐");
//			data.setAge(7);
//			data.setSex("男");
//			data.setAddress("安徽省合肥市");
//			boolean createDocument = ElasticsearchUtil.createDocument(indexName, data);
//			LOG.info("为索引{}插入文档，结果{}", indexName, createDocument);
//			// 批量插入文档
//			List<DocumentModel> dataList = new ArrayList<DocumentModel>();
//			DocumentModel data2 = new DocumentModel();
//			data2.setName("ww");
//			data2.setAge(28);
//			data2.setSex("女");
//			data2.setAddress("安徽省苏州市");
//			dataList.add(data);
//			dataList.add(data2);
//			boolean createBathDocument = ElasticsearchUtil.createBathDocument(indexName, dataList);
//			LOG.info("批量向索引{}添加文档，结果{}", indexName, createBathDocument);
//			// 更新文档
//			Map<String, Object> updateMap = new HashMap<>();
//			updateMap.put("age", 18);
//			boolean updateDocumentWithId = ElasticsearchUtil.updateDocumentWithId(indexName, "1001", updateMap, data.getClass());
//			LOG.info("为索引{}更新文档，结果{}", indexName, updateDocumentWithId);
			// 查询索引下全部文档
//			List<DocumentModel> allDocumentList = ElasticsearchUtil.queryAllIndexDocument(indexName, DocumentModel.class);
//			LOG.info("查新索引{}下全部文档allDocumentList={}", indexName, JSON.toJSONString(allDocumentList));
			
//			boolean closeIndex = ElasticsearchUtil.closeIndex("shopping");
//			LOG.info("关闭索引{}，结果{}", "shopping", closeIndex);
			
//			boolean openIndex = ElasticsearchUtil.openIndex("shopping");
//			LOG.info("开启索引{}，结果{}", "shopping", openIndex);
			
			boolean putAlias = ElasticsearchUtil.putAlias("shopping", "shopping-alias");
			LOG.info("索引{}添加别名结果{}", "shopping", putAlias);
			
//			boolean deleteAlias = ElasticsearchUtil.deleteAlias("shopping", "shopping-alias");
//			LOG.info("索引{}删除别名结果{}", "shopping", deleteAlias);
			
		} catch (ElasticsearchException | IOException e) {
			e.printStackTrace();
		}
	}
}
